<!--
 * @Description:
 * @Author: ZachGmy
 * @Date: 2020-09-03 09:57:40
 * @LastEditors: ZachGmy
 * @LastEditTime: 2022-06-15 09:49:13
-->
<template>
  <div class="category">
    <div class="btn">
      <el-button type="primary" icon="el-icon-edit" @click.native="addDialog()">
        新增</el-button
      >
    </div>
    <div class="table">
      <el-table :data="cateData" style="width: 100%" >
        <el-table-column label="序号" width="180" align="center">
          <template slot-scope="scope">
            <i class="el-icon-s-flag"></i>
            <span style="margin-left: 10px">{{ scope.row.no }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分类名称" width="280" align="center">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>名称: {{ scope.row.name }}</p>
              <p>简介: {{ scope.row.description }}</p>
              <div slot="reference" class="name-wrapper">
                <span>{{ scope.row.name }}</span>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="分类简介" width="480" align="center">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>名称: {{ scope.row.name }}</p>
              <p>简介: {{ scope.row.description }}</p>
              <div slot="reference" class="name-wrapper">
                <span v-if="scope.row.description != null">{{
                  scope.row.description
                }}</span>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="dialog"></div>
    <el-dialog
      title="录入分类信息"
      :visible.sync="dialogFormVisible"
      prop="name"
    >
      <el-form :model="form" :rules="formRules" ref="form">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="addname">
          <el-input v-model="form.addname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" :label-width="formLabelWidth">
          <el-input v-model="form.addtext"></el-input>
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="addno">
          <el-input v-model="form.addno"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click.native="addCateData()"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="修改分类信息" :visible.sync="dialogFormVisible1">
      <el-form :model="form1" :rules="form1Rules" ref="form1">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="upname">
          <el-input v-model="form1.upname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" :label-width="formLabelWidth" prop="uptext">
          <el-input v-model="form1.uptext"></el-input>
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="upno">
          <el-input v-model="form1.upno"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close1()">取 消</el-button>
        <el-button type="primary" @click.native="upCateData()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <i class="el-icon-warning"></i>
      <span>此操作将永久删除该数据, 是否继续?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delCateData(delid)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get } from "@/utils/request";
import { post } from "@/utils/request";
export default {
  data() {
    return {
      cateData: [],
      page: "1",
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogVisible: false,
      form: {
        addname: "",
        addno: "",
        addtext: "",
      },
      form1: {
        upno: "",
        upname: "",
        upid: "",
        uptext: "",
      },
      formRules: {
        // 新闻标题的表单验证规则
        addno: [
          { required: true, message: "请输入新闻分类序号", trigger: "blur" },
        ],
        // 下拉选择框的表单验证规则
        addname: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      form1Rules: {
        // 新闻标题的表单验证规则
        upno: [
          { required: true, message: "请输入新闻分类序号", trigger: "blur" },
        ],
        // 下拉选择框的表单验证规则
        upname: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      delid: "",

      formLabelWidth: "120px",
    };
  },
  created() {
    this.getCategoryData();
  },
  methods: {
    //查询分类信息
    async getCategoryData() {
      let parmas = {
        page: this.page,
      };
      let res = await get("/category/findAll", parmas);
      console.log(res.data);
      this.cateData = res.data;
    },
    //单击编辑按钮事件
    handleEdit(index, row) {
      console.log(index, row);
      this.form1.upno = row.no;
      this.form1.upid = row.id;
      this.form1.upname = row.name;
      this.form1.uptext = row.description;
      //this.form1 = {...row};
      this.dialogFormVisible1 = true;
    },
    //单击删除事件
    handleDelete(index, row) {
      console.log(index, row);
      this.delid = row.id;
      this.dialogVisible = true;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //删除分类数据
    async delCateData(delid) {
      console.log(delid);
      let parmas = {
        id: delid,
      };
      let res = await get("/category/deleteById", parmas);
      console.log(res);
      if (res.status == 200) {
            //提交成功之后给一个提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 关闭模态框
            this.dialogVisible = false;
            // 刷新页面
            this.getCategoryData();
          } else {
            // 失败也给一个提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "error",
            });
          }
    },
    //单击添加按钮事件
    addDialog() {
      this.dialogFormVisible = true;
    },
    //添加新分类数据
    addCateData() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 将表单对象提交到对应的接口中
          let data = {
            name: this.form.addname,
            description: this.form.addtext,
            no: this.form.addno,
            parentId: this.parentId,
          };
          let res = await post("/category/saveOrUpdate", data);
          if (res.status == 200) {
            //提交成功之后给一个提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 关闭模态框
            this.dialogFormVisible = false;
            // 刷新页面
            this.getCategoryData();
          } else {
            // 失败也给一个提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "error",
            });
          }
        } else {
          return false;
        }
      });
      // let data = {
      //   name: this.form.addname,
      //   description: this.form.addtext,
      //   no: this.form.addid,
      //   parentId: this.parentId,
      // };
      // let res = await post("/category/saveOrUpdate", data);
      // console.log(res);
      // this.dialogFormVisible = false;
      // this.getCategoryData();
    },
    //修改分类数据
    upCateData() {
      this.$refs["form1"].validate(async (valid) => {
        if (valid) {
          // 将表单对象提交到对应的接口中
          let data = {
            id: this.form1.upid,
            name: this.form1.upname,
            description: this.form1.uptext,
            no: this.form1.upno,
          };
          let res = await post("/category/saveOrUpdate", data);
          if (res.status == 200) {
            //提交成功之后给一个提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 关闭模态框
            this.dialogFormVisible1 = false;
            // 刷新页面
            this.getCategoryData();
          } else {
            // 失败也给一个提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "error",
            });
          }
        } else {
          return false;
        }
      });
      // let data = {
      //   id: this.form1.upid,
      //   name: this.form1.upname,
      //   description: this.form1.uptext,
      //   no: this.form1.upno,
      // };
      // let res = {};
      // res = await post("/category/saveOrUpdate", data);
      // console.log(res);
      // this.dialogFormVisible1 = false;
      // if (res.status == 200) {
      //   this.$notify({
      //     title: "成功",
      //     message: "修改数据成功",
      //     type: "success",
      //   });
      // } else {
      //   this.$notify({
      //     title: "失败",
      //     message: "修改数据失败",
      //     type: "error",
      //   });
      // }
      // this.getCategoryData();
    },
    //点击模态框的取消按钮事件
    close(){
      //清空没有提交的表单数据
      this.form = {};
      this.dialogFormVisible=false;
      //重启模态框
      this.$refs.form.clearValidate(); 
    },
    close1(){
      this.dialogFormVisible1=false;
      this.$refs.form1.clearValidate();
    }
  },
};
</script>

<style scoped>
.table {
  margin-top: 5px;
}
</style>